/*
 * Copyright (c) 2019-2020, https://api.iot.10086.cn/(https://gitee.com/cmcc-iot-api/iot-card-open-application/).
 */
package cn.iot.card.open.sim.controller;

import cn.iot.card.open.constants.Constants;
import cn.iot.card.open.constants.ErrorCode;
import cn.iot.card.open.context.LoginContextHolder;
import cn.iot.card.open.context.model.LoginUser;
import cn.iot.card.open.exception.enums.BizExceptionEnum;
import cn.iot.card.open.modular.entity.AccountCard;
import cn.iot.card.open.modular.entity.Card;
import cn.iot.card.open.modular.entity.UserGroupRl;
import cn.iot.card.open.modular.log.ApiBussinessLog;
import cn.iot.card.open.modular.service.AccountCardService;
import cn.iot.card.open.modular.service.CardService;
import cn.iot.card.open.modular.service.UserService;
import cn.iot.card.open.pojo.page.LayuiPageFactory;
import cn.iot.card.open.pojo.page.LayuiPageInfo;
import cn.iot.card.open.sim.dto.siminfo.SimInfoDTO;
import cn.iot.card.open.sim.service.GeneralSmsService;
import cn.iot.card.open.util.CommonUtil;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 物联卡综合信息-短信控制器
 *
 * @author 何光鑫
 * @date 20200316 21:40:06
 * @modify 20200316 何光鑫 v1.1.2 创建
 * @since v1.1.2
 */
@Controller
@RequestMapping("/generalSms")
public class GeneralSmsController {

    /**
     * 页面路径前缀
     */
    private static String PREFIX = "/modular/sim/general/sms/";

    @Autowired
    private GeneralSmsService generalSmsService;

    @Autowired
    private CardService cardService;

    @Autowired
    private UserService userService;

    @Autowired
    private AccountCardService accountCardService;

    /**
     * 查询套餐内用量信息
     *
     * @param msisdn 物联卡号码
     * @param iccid 集成电路卡识别码
     * @param imsi 国际移动用户识别码
     * @return 返回 layui page info
     * @throws IOException
     * @author 何光鑫
     * @date 20200316 21:40:06
     * @modify 20200316 何光鑫 v1.1.2 创建
     * @since v1.1.2
     */
    @RequestMapping("/querySimSmsMargin")
    @ResponseBody
    public LayuiPageInfo querySimSmsMargin(@RequestParam(required = false) String msisdn, @RequestParam(required = false) String iccid, @RequestParam(required = false) String imsi) throws IOException {
        Map<String, Object> map = new HashMap<>();
        String cardType = "";
        String cardNum = "";
        if(ToolUtil.isNotEmpty(msisdn)){
            map.put(Constants.REQUEST_PARAM_MSISDN,msisdn);
            cardType = Constants.REQUEST_PARAM_MSISDN;
            cardNum = msisdn;
        }else if(ToolUtil.isNotEmpty(iccid)){
            map.put(Constants.REQUEST_PARAM_ICCID,iccid);
            cardType = Constants.REQUEST_PARAM_ICCID;
            cardNum = iccid;
        }else if(ToolUtil.isNotEmpty(imsi)){
            map.put(Constants.REQUEST_PARAM_IMSI,imsi);
            cardType = Constants.REQUEST_PARAM_IMSI;
            cardNum = imsi;
        }else{
            return LayuiPageFactory.createEmptyPageInfo();
        }
        map.put(Constants.RESPONSE_DELETED,Constants.RESPONSE_DELETED_FALSE);
        List<Card> cards = cardService.getBaseMapper().selectByMap(map);
        if(ToolUtil.isEmpty(cards)){
            return LayuiPageFactory.createBizExceptionMsg(BizExceptionEnum.CARD_NOT_EXIT_IN_SYSTEM);
        }
        Card card = cards.get(0);
        // 验证当前用户是否有卡权限
        LoginUser loginUser = LoginContextHolder.getContext().getUser();
        UserGroupRl userGroupRl = userService.getUserGroupRlByUserAndCard(loginUser.getId(), card.getCardId());
        if (null == userGroupRl) {
            return LayuiPageFactory.createBizExceptionMsg(BizExceptionEnum.CARD_NOT_BELONG_USER);
        }
        Map<String, Object> cardDtoMap = cardService.getCardInfo(card.getCardId());
        Object accountIdObj = cardDtoMap.get(Constants.REQUEST_ACCOUNT_ID);
        if (ToolUtil.isEmpty(accountIdObj)) {
            // 当前卡未绑定任何账户
            return LayuiPageFactory.cteateErrorMsg(ErrorCode.ERR11047);
        }
        String accountId = String.valueOf(accountIdObj);
        String transId = CommonUtil.generateTransId(String.valueOf(accountId));
        Map<String, String> parameterMap = new HashMap<>();
        parameterMap.put(cardType,cardNum);
        return generalSmsService.querySimSmsMargin(transId,accountId,parameterMap);
    }

    /**
     * 跳转到短信状态重置界面
     *
     * @return 返回 string
     * @author 何光鑫
     * @date 20200316 21:40:06
     * @modify 20200316 何光鑫 v1.1.2 创建
     * @since v1.1.2
     */
    @RequestMapping("/toResetSimSmsStatus")
    public String toResetSimSmsStatus() {
        return PREFIX + "resetSimSmsStatus.html";
    }


    /**
     * 短信状态重置
     *
     * @param simInfoDTO
     * @return 返回 response data
     * @throws Exception
     * @author 何光鑫
     * @date 20200316 21:40:01
     * @modify 20200316 何光鑫 v1.1.2 创建
     * @since v1.1.2
     */
    @RequestMapping("/resetSimSmsStatus")
    @ApiBussinessLog(value = "物联卡短信状态重置")
    @ResponseBody
    public ResponseData resetSimSmsStatus(SimInfoDTO simInfoDTO) throws Exception {
        String cardType = simInfoDTO.getCardType();
        String cardNum = simInfoDTO.getCardNum();
        Card card = cardService.findCard(cardType, cardNum);
        if (null == card) {
            throw new ServiceException(BizExceptionEnum.CARD_NOT_EXIT_IN_SYSTEM);
        }
        LoginUser loginUser =  LoginContextHolder.getContext().getUser();
        UserGroupRl userGroupRl = userService.getUserGroupRlByUserAndCard(loginUser.getId(), card.getCardId());
        if (null == userGroupRl) {
            throw new ServiceException(Integer.valueOf(BizExceptionEnum.CARD_NOT_BELONG_USER.getCode()),BizExceptionEnum.CARD_NOT_BELONG_USER.getMessage());
        }

        AccountCard accountCard = accountCardService.getBaseMapper().findByCardId(card.getCardId());
        String accountId = String.valueOf(accountCard.getAccountId());
        String transId = CommonUtil.generateTransId(accountId);

        return generalSmsService.resetSimSmsStatus(accountId, transId, cardType, cardNum);
    }
}
